package com.example.yuhuaqiu.myqq;

/**
 * Created by Administrator on 2016/5/24.
 */
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.springfairytale.qq.common.Constant;
import com.springfairytale.qq.common.Protocol;

import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/14.
 */
public class UserRunnable implements Runnable{
    private Socket socket;
    private Protocol protocol;
    private String userid;
    private String password;
    private AppCompatActivity activity;


    public UserRunnable(String userid, String password, AppCompatActivity activity) {
        this.userid = userid;
        this.password = password;
        this.activity = activity;
    }

    @Override
    public void run() {
        try {
            socket  = new Socket("192.168.1.115",8888);
            protocol =  new Protocol("01");
            protocol.setAction(Constant.LOGIN);
            Map<String,Object> msg = new HashMap<>();
            msg.put("who",userid);
            msg.put("pwd",password);
            protocol.setMsg(msg);
            protocol.sendMessage(socket);
            Protocol reProtocol = Protocol.getProtocol(socket);
            Log.i("lwy",userid);
            Log.i("lwy",password);
            Log.i("lwy",reProtocol.getAction()+"");
            Log.i("lwy",(String)reProtocol.getMsg().get("result"));
            String result = (String)reProtocol.getMsg().get("result");
            if("succ".equals(result)) {
                Intent intent = new Intent(activity,MessageListActivity.class);
                activity.startActivity(intent);
                activity.finish();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

